package com.example.jbmsuppliers.sinopec.orderaddress;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.ActivityUse;
import com.example.jbmsuppliers.sinopec.adapter.MyRecy_photoAdapter;
import com.example.jbmsuppliers.sinopec.net.NetAddRess;
import com.example.jbmsuppliers.sinopec.net.busheven.UtlisNetWork;
import com.example.jbmsuppliers.sinopec.utlis.UserAddress;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.example.jbmsuppliers.sinopec.view.MyToast;
import com.example.permission.myjbm.utils.ScreenAsset;
import com.example.weather.myframe.PopupSelectImage;
import com.jbm.jbmsupplier.util.IOrder;
import com.jbm.jbmsupplier.util.Media;

import java.io.File;
import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.example.jbmsuppliers.R.id.baoxiu_sydzgd;


/**
 * Created by 家帮忙 on 2016/9/26.
 */
public class MyAddRessRepair extends ActivityUse {

    @InjectView(R.id.header_title)
    TextView headerTitle;
    @InjectView(R.id.header_right)
    TextView headerRight;
    @InjectView(R.id.header_back)
    ImageView headerBack;
    @InjectView(R.id.release_desc)
    EditText releaseDesc;
    @InjectView(R.id.recy_repairs_pho)
    RecyclerView recyRepairsPho;
    @InjectView(R.id.image_repairs_zhaopian)
    ImageView imageRepairsZhaopian;
    @InjectView(baoxiu_sydzgd)
    TextView baoxiuSydzgd;
    private ArrayList<Bitmap> listrecys;

    private int showaddress = 0;
    private final int LOADADDRESS = 0000;
    private final int GAINADDRESS = 0001;
    private final int REPAIRS = 0002;
    private final int MEDIAIPC = 0003;
    String name = "加载中...";

    private int mode;
    private int screenWidth;
    private File file;
    private DialogLoading baseLoader;

    @Override
    protected int setContentView1() {
        return R.layout.repairs_main_baoxiu;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        listrecys = new ArrayList<Bitmap>();
        recyRepairsPho.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
        handler.sendMessage(handler.obtainMessage(LOADADDRESS));
        mode = UtlisNetWork.getnetwokstate(MyAddRessRepair.this);
        screenWidth= ScreenAsset.getScreenWidth(MyAddRessRepair.this);
    //    file = new File(Environment.getExternalStorageDirectory() + "/MallImage");
        headerTitle.setText("报修");
        headerRight.setText("提交");

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == 102) {
            UserAddress date = (UserAddress) data.getSerializableExtra("data");
            showaddress = date.id;
            baoxiuSydzgd.setText("用户名:"+date.mContacts+"    "+"手机号:"+date.mMobile+"\n"+date.mAddress);
        } else if (requestCode == 100) {
             file = (File) data.getSerializableExtra("file");
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inSampleSize = 4;
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            try {
//                FileOutputStream os = new FileOutputStream(file);
//                bitmap.compress(Bitmap.CompressFormat.JPEG, 85, os);
                listrecys.add(bitmap);
                if (listrecys.size()>-1){
                    imageRepairsZhaopian.setVisibility(View.GONE);
                    recyRepairsPho.setAdapter(new MyRecy_photoAdapter(this, listrecys,imageRepairsZhaopian));}
//                os.close();
            } catch (Exception e) {
                Log.e("main", Log.getStackTraceString(e));
            }

        }
    }

    private DialogLoading dialogs;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case REPAIRS: {
                    final IOrder id = (IOrder) msg.obj;
                    if (id.back) {
                        if (dialogs != null) {
                            dialogs.dismiss();
                        }
                        if (listrecys.size() > -1) {
                            if (dialogs != null) {
                                dialogs.dismiss();
                            }
                            Media media = new Media();
                            media.mtype = "pic";
                            media.filepath = file.getPath();
                            media.filename =file.getName();
                            NetAddRess netAddRess = new NetAddRess(handler);
                            netAddRess.setMediaipc(id.order_id, media, MEDIAIPC);
                        }
                        MyToast.show(MyAddRessRepair.this,"报修成功",0);
                        handler.sendEmptyMessageAtTime(GAINADDRESS,1000);
                    } else if (id.equals("time out")) {
                        if (dialogs != null) {
                            dialogs.dismiss();
                        }
                        hint(id.errorinfo);
                    } else {
                        if (dialogs != null) {
                            dialogs.dismiss();
                        }
                        hint(id.errorinfo);
                        return;
                    }


                    break;
                }
                case MEDIAIPC: {
                    Media medi = (Media) msg.obj;
                    if (dialogs != null) {
                        dialogs.dismiss();
                    }
                    break;
                }
                case GAINADDRESS:{
                    finish();
                    break;
                }

            }
            super.handleMessage(msg);
        }
    };  File mTmpFile;
    @OnClick({R.id.header_right, R.id.header_back, R.id.image_repairs_zhaopian, baoxiu_sydzgd})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.header_right:
                    if (!releaseDesc.getText().toString().equals("")) {
                        if (showaddress != 0) {
                        if (mode == 0 || mode == 1) {

                            if (releaseDesc.getText().length()>100){
                                hint("报修内容过多请修改后重试");
                            }else {
                                NetAddRess netAddRess = new NetAddRess(handler);
                                netAddRess.setRepais(showaddress, releaseDesc.getText().toString(), REPAIRS);
                                dialogs = new DialogLoading(MyAddRessRepair.this, name,false);
                                dialogs.show();
                            }



                        } else if (mode == -1) {
                            hint("网络无连接，请稍后再试！");
                        }
                        }else{
                            hint("请选择地址");
                        }
                    } else{
                        hint("报修内容不能为空并且不能太少内容");
                    }
                
                break;
            case R.id.header_back:
                finish();
                break;
            case R.id.image_repairs_zhaopian:

//                if (listrecys.size()<0) {
                    Intent intent = new Intent(MyAddRessRepair.this, PopupSelectImage.class);
                    startActivityForResult(intent, 100);
//                }else {
//                    imageRepairsZhaopian.setVisibility(View.GONE);
//
//                }


                break;
            case baoxiu_sydzgd:
                startActivityForResult(new Intent(MyAddRessRepair.this, ManageAddressActivity.class), 102);
                break;

    }

    }


}
